package vn.sms.lazylove.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.sms.lazylove.CreateSmsActivity;
import vn.sms.lazylove.R;
import vn.sms.lazylove.object.Sms;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class SmsAdapter extends BaseAdapter {

	private Context mContext;
	private Activity mActivity;
	private LayoutInflater mInflater;
	private List<Sms> mListObject = new ArrayList<Sms>();
	private List<Sms> mListDeleted = new ArrayList<Sms>();
	private boolean mDeleteMode = false;

	public SmsAdapter(Activity _activity) {
		mActivity = _activity;
	}

	public SmsAdapter(Activity _activity, List<Sms> _listObject) {
		mContext = _activity;
		mActivity = _activity;
		mInflater = LayoutInflater.from(_activity);
		if (mListObject != null)
			mListObject = _listObject;
		if (mListDeleted == null)
			mListDeleted = new ArrayList<Sms>();
		else
			mListDeleted.clear();
	}

	public List<Sms> getListDeleted() {
		return mListDeleted;
	}

	public void notifyListObjectChanged(boolean inDeleteMode) {
		mDeleteMode = inDeleteMode;
		notifyDataSetChanged();
	}

	public void setData(List<Sms> objs) {
		mListObject = objs;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mListObject.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListObject.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		final Object obj = mListObject.get(position);
		final Sms sms = (Sms) obj;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_listview_sms, null);
			holder = new ViewHolder();
			holder.message = (TextView) convertView
					.findViewById(R.id.txtSmspatent);
			holder.btnDelete = (ImageButton) convertView
					.findViewById(R.id.btn_delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (obj instanceof Sms) {
			holder.message.setText(sms.message);
			holder.btnDelete.setVisibility(ImageButton.GONE);
		}

		if (mDeleteMode) {
			holder.btnDelete.setVisibility(ImageButton.VISIBLE);
			holder.btnDelete
					.setBackgroundResource(R.drawable.selector_button_delete);
			holder.btnDelete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// remove item
					mListObject.remove(sms);
					mListDeleted.add(sms);
					notifyDataSetChanged();
				}
			});
		} else {
			holder.btnDelete.setBackgroundResource(R.drawable.ic_arrow_grey);
		}

		convertView.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Bundle b = new Bundle();
				b.putString("sms", sms.message);
				Intent myIntent = new Intent();
				myIntent.putExtras(b);
				mActivity.setResult(Activity.RESULT_OK, myIntent);
				mActivity.finish();
			}
		});

		return convertView;
	}

	class ViewHolder {
		TextView message;
		ImageButton btnDelete;
	}

}
